package com.liudadan.ioc_01;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidPooledConnection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.sql.PreparedStatement;
import java.sql.SQLException;

@Component
public class UserDao {
    @Autowired
    private DruidDataSource dataSource;

    public void insert() throws SQLException {
        DruidPooledConnection connection = dataSource.getConnection();

        String sql = "insert into sys_company (company_name) values (?);";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setObject(1,"极兔快递");
        int i = preparedStatement.executeUpdate();
        if(i>0){
            System.out.println("数据插入成功");
        }else {
            System.out.println("数据插入失败");
        }
        preparedStatement.close();
        connection.close();
    }
}
